package application

import (
	"hong/uniapp_server/application/dto"
	"hong/uniapp_server/domain/user"

	"github.com/gofiber/fiber/v2"
)

type AuthController struct {
	userService *user.UserService
}

func NewAuthController(userService *user.UserService) *AuthController {
	return &AuthController{userService: userService}
}

func (c *AuthController) Login(ctx *fiber.Ctx) error {
	var req struct {
		Code string `json:"code"`
	}
	if err := ctx.BodyParser(&req); err != nil {
		return ctx.JSON(dto.Response{
			Code:    dto.ResponseParamsError,
			Message: err.Error(),
		})
	}
	user, err := c.userService.Login(req.Code)
	if err != nil {
		return ctx.JSON(dto.Response{
			Code:    dto.ResponseParamsError,
			Message: err.Error(),
		})
	}
	return ctx.JSON(dto.Response{
		Code: dto.ResponseSuccess,
		Data: user.WxOpenId,
	})
}
